package top.meeting.room.module.system.model;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import top.jfinal.jwtTokenPlugin.Bean.IJwtAble;
import top.meeting.room.baseModel.BaseUser;
import top.meeting.room.common.model.Files;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/22
 * FOR : UserBean
 */
public class User extends BaseUser<User> implements IJwtAble {

    public static final User dao = new User().dao();
    /**
     * 设置删除某个用户标识符
     */
    public static final Integer makeDeleted = 1;
    /**
     * 设置恢复某个用户标识符
     */
    public static final Integer makeRecover = 0;
    /**
     * SqlParas And Caches
     */
    public static final String user_login = "user.login";
    public static final String user_saveInfo = "user.saveInfo";
    public static final String user_getRole = "user.getRole";
    public static final String user_setRole = "user.setRole";
    public static final String user_isExist = "user.isExist";
    public static final String user_getModel = "user.getModel";
    public static final String user_pageModel = "user.pageModel";
    public static final String user_findByIds = "user.findByIds";
    public static final String user_getRecord = "user.getRecord";
    public static final String user_pageRecord = "user.pageRecord";
    public static final String user_getPicture = "user.getPicture";
    public static final String user_toStateByIds = "user.toStateByIds";
    public static final String user_resetPassword = "user.resetPassword";
    public static final String user_findByRoles = "user.findByRoles";
    public static final String user_findSp = "user.findSp";

    public static final String user_getRoles = "user.getRoles";
    public static final String user_getForces = "user.getForces";

    public static final String TABLE_NAME = "t_user";

    /**
     * 支持 JwtUser生成的roles
     *
     * @return
     */
    @Override
    public List<String> getRoles() {
        List<String> retVal = CacheKit.get(user_getRoles, String.valueOf(this.getId()));
        if (retVal == null) {
            Record role = this.getRole();
            String roleName = role == null ? null : role.get(Role.roleName);
            retVal = Arrays.asList(roleName);
            CacheKit.put(user_getRoles, String.valueOf(this.getId()), retVal);
        }
        return retVal;
    }

    /**
     * 支持 JwtUser生成的forces
     *
     * @return
     */
    @Override
    public List<String> getForces() {
        List<String> retVal = CacheKit.get(user_getForces, String.valueOf(this.getId()));
        if (retVal == null) {
            Record role = this.getRole();
            retVal = new LinkedList<String>();
            List<Record> records = role == null ? null : ((List<Record>) role.get("forceList"));
            if (records != null) {
                for (Record forceRoot : records) {
                    if (forceRoot != null) {
                        retVal.add(forceRoot.get(Force.forceName));
                        List<Record> forceMenus = forceRoot.get("sonForces");
                        if (forceMenus != null && !forceMenus.isEmpty()) {
                            for (Record forceMenu : forceMenus) {
                                if (forceMenu != null) {
                                    retVal.add(forceMenu.get(Force.forceName));
                                    List<Force> forceButtons = forceMenu.get("sonForces");
                                    if (forceButtons != null && !forceButtons.isEmpty()) {
                                        for (Force forceButton : forceButtons) {
                                            if (forceButton != null) {
                                                retVal.add(forceButton.get(Force.forceName));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            CacheKit.put(user_getForces, String.valueOf(this.getId()), retVal);
        }
        return retVal;
    }

    /**
     * 支持 JwtUser生成的上次修改密码时间
     *
     * @return
     */
    @Override
    public Date getLastModifyPasswordTime() {
        return super.getLastModifyPasswordTime();
    }

    /**
     * 获取角色
     *
     * @return
     */
    public Record getRole() {
        Record retVal = CacheKit.get(user_getRole, String.valueOf(this.getRoleId()));
        if (retVal == null) {
            Role role = Role.dao.findById(this.getRoleId());
            retVal = role == null ? null : role.toBack();
            CacheKit.put(user_getRole, String.valueOf(this.getRoleId()), retVal);
        }
        return retVal;
    }


    /**
     * 重载 toRecord
     *
     * @return
     */
    public Record toMe() {
        Record retVal = super.toRecord();
        return retVal == null ? null : retVal.set("role", getRole())
                .set("forces", getForces()).remove(User.password);
    }

    /**
     * 前台
     *
     * @return
     */
    public Record toDto() {
        Record retVal = super.toRecord();
        return retVal == null ? null : retVal.remove(User.password);
    }

    /**
     * 转化为带角色的DTO
     *
     * @return
     */
    public Record toUserWithRole() {
        Record retVal = super.toRecord();
        return retVal == null ? null :
                retVal.remove(User.password).set("role_name", getRole() == null ? "" : this.getRole().get(Role.roleName));
    }
}
